// 校验手机号码
export const validPhone = (rule, value, callback) => {
  const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
  if (!value) {
      callback(new Error('请输入手机号码'))
  } else if (!reg.test(value)) {
      callback(new Error('请输入正确的11位手机号码'))
  } else {
      callback()
  }
}
// 校验IP地址
export const validIp = (rule, value, callback) => {
    const reg = /(?=(\b|\D))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))(?=(\b|\D))/
    if (!value) {
        callback(new Error('请输入ip地址'))
    } else if (!reg.test(value)) {
        callback(new Error('请输入正确的ip地址'))
    } else {
        callback()
    }
  }
// 校验数字
export const validNum = (rule, value, callback) => {
    const reg = /^\d+$/
    if (!value) {
        callback(new Error('请输入数字'))
    } else if (!reg.test(value)) {
        callback(new Error('请输入数字'))
    } else if (value == 0) {
        callback(new Error('次数不能为0'))
    } else if (value > 100) {
        callback(new Error('次数不能超过100'))
    } else {
        callback()
    }
}
//转换时间格式
export const newTime = (timestamp) => {
    let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + "-";
    let M =  (date.getMonth() + 1 < 10    ? "0" + (date.getMonth() + 1)    : date.getMonth() + 1) + "-";
    let D =  date.getDate() + " " < 10    ? "0" + date.getDate() + " "    : date.getDate() + " ";
    let h =  date.getHours() + ":" < 10    ? "0" + date.getHours() + ":"    : date.getHours() + ":";
    let m =  date.getMinutes() + ":" < 10    ? "0" + date.getMinutes() + ":"    : date.getMinutes();
    // let s =  date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();  
    return Y + M + D + h + m;
}

export const timestampToTime = (res) => {
    let date = new Date(res); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear();
    let M =  (date.getMonth() + 1 < 10    ? "0" + (date.getMonth() + 1)    : date.getMonth() + 1);
    let D =  date.getDate() + " " < 10    ? "0" + date.getDate()    : date.getDate();
    let h =  date.getHours() + ":" < 10    ? "0" + date.getHours() + ":"    : date.getHours() + ":";
    let m =  date.getMinutes() + ":" < 10    ? "0" + date.getMinutes() + ":"    : date.getMinutes() + ":";
    let s =  date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    return Y + '年'+ M + '月'+ D +'日'+ h + m + s;
}